<?php

namespace App\Admin\Controllers\Tool;

use App\Admin\Repositories\Tool\FileUrl;
use Dcat\Admin\Form;
use Dcat\Admin\Grid;
use Dcat\Admin\Show;
use Dcat\Admin\Http\Controllers\AdminController;
use App\Models\Tool\FileUrl as FileUrlModel;
use App\Models\Category\CategoryModule;
class FileUrlController extends AdminController
{
    protected $title = '资源地址';

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Grid::make(new FileUrl(), function (Grid $grid) {
            $grid->column('id')->sortable();
            $grid->column('domain', '域名');
            $grid->column('url', '资源地址');
            $grid->column('module', '模块')->display(function ($module) {
                return CategoryModule::$moduleFileMap[$module] ?? '未知模块';
            });
            $grid->column('sign', '模块标识');
            $grid->column('is_category', '是否开启目录')->display(function ($is_category) {
                return FileUrlModel::$IsCategoryMap[$is_category];
            });
            $grid->column('is_type', '是否开启类型')->display(function ($is_type) {
                return FileUrlModel::$IsTypeMap[$is_type];
            });

            $grid->filter(function (Grid\Filter $filter) {
                $filter->panel();
                $filter->expand();
                $filter->equal('id')->width(2);
                $filter->like('domain', '域名')->width(3);
                $filter->like('sign', '模块标识')->width(2);
                $filter->equal('module', '模块')->select($this->getModuleOptions())->width(3);
            });
        });
    }

    /**
     * Make a show builder.
     *
     * @param mixed $id
     *
     * @return Show
     */
    protected function detail($id)
    {
        return Show::make($id, new FileUrl(), function (Show $show) {
            $show->field('id');
            $show->field('domain', '域名');
            $show->field('url', '资源地址');
            $show->field('module', '模块')->as(function ($module) {
                return $this->getModuleOptions()[$module] ?? '未知模块';
            });
            $show->field('sign', '模块标识');
            $show->field('is_category', '是否开启目录')->as(function ($is_category) {
                return FileUrlModel::$IsCategoryMap[$is_category];
            });
            $show->field('is_type', '是否开启类型')->as(function ($is_type) {
                return FileUrlModel::$IsTypeMap[$is_type];
            });
            $show->field('created_at', '创建时间');
            $show->field('updated_at', '更新时间');
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Form::make(new FileUrl(), function (Form $form) {
            $form->text('domain', '域名')->required();
            $form->text('url', '资源地址')->required();
            $form->text('sign', '模块标识')->required();
            $form->select('module', '模块')->options($this->getModuleOptions())->required();
            $form->switch('is_category', '是否开启目录');
            $form->switch('is_type', '是否开启类型');
        });
    }

    /**
     * 获取模块选项
     */
    protected function getModuleOptions(): array
    {
        return CategoryModule::$moduleFileMap;
    }
}
